<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../static/css/organization/thesame.css" media="all">
    <link rel="stylesheet" href="../static/css/staff/css/footer.css" media="all">
    <link rel="stylesheet" href="../static/css/staff/css/rightCon.css" media="all">
    <link rel="stylesheet" href="../static/userinfoPrivate/layer/theme/default/layer.css" media="all">
    <link rel="stylesheet" href="../static/css/staff/css/tree.css" media="all">
    <style>
        .add-mess-child > div.add-mess-form > p {
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 0 10px;
        }

        .add-mess-child > div.add-mess-form-width {
            width: 750px
        }
    </style>
</head>

<body class="layui-layout-body">
<input type="hidden" id="staffNo" th:value="${staffNo!=null ?staffNo:''}">
<div class="layui-body">
    <div class="add-title pad25px"><span class="mr10px cursor return"><img src="../static/css/staff/img/return_icon.png"></span><span id="titi">新建员工</span></div>
    <div class="new-add-box new-add-box-width bgWhite box-shadows border-radius4">
        <div class="new-add-box-scroll">
            <div class="add-mess add-mess-mb mar25px">
                <article class="overflow">
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>姓名：</label>
                        </div>
                        <div class="add-mess-form">
                            <p><input autocomplete="off" type="text" id="staffName" maxlength="20" placeholder="请输入"></p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>身份证号：</label>
                        </div>
                        <div class="add-mess-form">
                            <p><input autocomplete="off" type="text" id="idCard" maxlength="18" placeholder="请输入"></p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>性别：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <select name="" id="sex" class="form-select">
                                    <option value="">请选择</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label>年龄：</label>
                        </div>
                        <div class="add-mess-form no-click">
                            <p><input autocomplete="off" type="text" id="age" placeholder="暂无" disabled=""></p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>学历：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <select name="" id="education" class="form-select">
                                    <option value="">请选择</option>
                                    <option value="2101">文盲</option>
                                    <option value="2102">小学</option>
                                    <option value="2103">初中</option>
                                    <option value="2104">中专</option>
                                    <option value="2105">高中</option>
                                    <option value="2106">本科</option>
                                    <option value="2107">硕士</option>
                                    <option value="2108">博士</option>
                                </select>
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>电话号码：</label>
                        </div>
                        <div class="add-mess-form">
                            <p><input autocomplete="off" type="text" id="phone" maxlength="11" placeholder="请输入"></p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>进院时间：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <input type="text" autocomplete="off" id="inDate" placeholder="请输入" class="layui-time test-item">
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label><em class="red">*</em>工龄（年）：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <input type="text" autocomplete="off" maxlength="2" id="workAge" placeholder="请输入">
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label>岗位情况：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <select name="" id="jobState" class="form-select">
                                    <option value="101">在职</option>
                                    <option value="102">返聘</option>
                                    <option value="103">兼职</option>
                                </select>
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label>职称：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <input type="text" autocomplete="off" maxlength="20" id="positional" placeholder="请输入">
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label>岗位：</label>
                        </div>
                        <div class="add-mess-form">
                            <p>
                                <input type="text" autocomplete="off" id="post" maxlength="20" placeholder="请输入">
                            </p>
                        </div>
                    </div>
                    <div class="add-mess-child">
                        <div>
                            <label>备注：</label>
                        </div>
                        <div class="add-mess-form add-mess-form-width">
                            <p>
                                <textarea rows="4" autocomplete="off" placeholder="请输入" maxlength="500" id="remarks"></textarea>
                            </p>
                        </div>
                    </div>
                </article>
            </div>
        </div>
    </div>
</div>
<div class="layui-footer layui-footer-one">
    <div class="btn-con textCenter" id="contain">
        <span class="btn-two cursor" id="save">提&nbsp;交</span>
    </div>
</div>
<script src="../static/js/jquery-2.1.3.min.js" charset="utf-8"></script>
<!--<script src="../static/staff/layui/layui.js" charset="utf-8"></script>-->
<script src="../static/userinfoPrivate/layer/layer.js" charset="utf-8"></script>
<script type="text/javascript" src="../static/laydate/laydate.js"></script>
<script>

    $(function () {
        $("#save").click(function () {
            save();
        })


        $("#idCard").blur(function () {
            // $("input").css("background-color","#D6D6FF");
            var idCard = $("#idCard").val();
            if (!isCardNo(idCard)) {
                layer.msg("身份证不合法")
                return;
            }
            var age = GetAge(idCard);
            if (!isNaN(age)) {
                $("#age").val(age);
            } else {
                $("#age").val("");
            }
        });

        $(".return").click(function () {
            // window.location.href = "../staff/toStaffInfoPage";
            window.history.back();
        })

        loadData();
    });

    lay('.test-item').each(function () {
        laydate.render({
            elem: '.test-item',
            trigger: 'click',
        });
    });

    //保存
    function save() {
        var staffNo = $("#staffNo").val();
        var staffName = $("#staffName").val();
        var idCard = $("#idCard").val();
        var sex = $("#sex").val();
        var age = $("#age").val();
        var education = $("#education").val();
        var phone = $("#phone").val();
        var inDate = $("#inDate").val();
        var workAge = $("#workAge").val();
        var jobState = $("#jobState").val();
        var positional = $("#positional").val();
        var post = $("#post").val();
        var remarks = $("#remarks").val();
        if (isNull(staffName)) {
            layer.msg("请输入姓名")
            return;
        }

        if (isNull(idCard)) {
            layer.msg("请输入身份证号")
            return;
        }

        if (isNull(sex)) {
            layer.msg("请选择性别")
            return;
        }


        if (isNull(education)) {
            layer.msg("请选择学历")
            return;
        }

        if (isNull(phone)) {
            layer.msg("请输入电话号码")
            return;
        }

        if (isNull(inDate)) {
            layer.msg("请选择进院日期")
            return;
        }

        if (isNull(workAge)) {
            layer.msg("请输入工龄")
            return;
        }
        var data = {
            staffName: staffName, idCard: idCard, sex: sex, education: education, age: age, phone: phone
            , inDate: inDate, workAge: workAge, jobState: jobState, positional: positional, post: post, staffNo: staffNo
            , remarks: remarks
        };
        $.ajax({
            url: "../staff/addStaffInfo",
            type: "post",
            async: false,
            dataType: "json",
            data: data,
            success: function (result) {
                if (result.code == 200) {
                    layer.alert("保存成功", function () {
                        window.location.href = "../staff/toStaffInfoPage";
                    })
                } else {
                    layer.msg(result.msg);
                }
            }
        })
    }

    //数据回显
    function loadData() {
        var staffNo = $("#staffNo").val();
        if (isNull(staffNo)) {
            return;
        }
        $("#contain").prepend("<span class=\"btn-one cursor\" onclick=\"deleteById('" + staffNo + "')\">删&nbsp;除</span>");
        $("#titi").text("编辑员工");
        var data = {staffNo: staffNo};
        $.ajax({
            url: "../staff/getStaffInfoById",
            type: "get",
            async: false,
            dataType: "json",
            data: data,
            success: function (data) {
                if (data.code == 200) {
                    $("#staffName").val(data.data.staffName);
                    $("#idCard").val(data.data.idCard);
                    $("#sex").val(data.data.sex);
                    $("#age").val(data.data.age);
                    $("#education").val(data.data.education);
                    $("#phone").val(data.data.phone);
                    $("#inDate").val(data.data.inDate);
                    $("#workAge").val(data.data.workAge);
                    $("#jobState").val(data.data.jobState);
                    $("#positional").val(data.data.positional);
                    $("#post").val(data.data.post);
                    $("#remarks").val(data.data.remarks);
                }
            }
        })
    }


    function deleteById(staffNo) {
        var data = {staffNo: staffNo};
        layer.confirm('是否删除该信息', {
            btn: ['确定', '取消']
        }, function () {
            layer.closeAll();
            $.ajax({
                url: "../staff/deleteById",
                type: "delete",
                async: false,
                dataType: "json",
                data: data,
                success: function (result) {
                    if (result.code == 200) {
                        layer.alert(result.msg, function () {
                            window.location.href = "../staff/toStaffInfoPage";
                        });
                    } else if (result.code == 101) {
                        layer.msg(result.msg);
                    }
                }
            })
        });
    }


    function isCardNo(card) {
        // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (reg.test(card) === false) {
            return false;
        }
        return true;
    }

    function GetAge(identityCard) {
        var len = (identityCard + "").length;
        if (len == 0) {
            return 0;
        } else {
            if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
            {
                return 0;
            }
        }
        var strBirthday = "";
        if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
        {
            strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
        }
        if (len == 15) {
            strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
        }
        //时间字符串里，必须是“/”
        var birthDate = new Date(strBirthday);
        var nowDateTime = new Date();
        var age = nowDateTime.getFullYear() - birthDate.getFullYear();
        //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
        if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
            age--;
        }
        return age;
    }


    function isNull(value) {
        return value == null || value === "" || value === undefined;
    }
</script>
</body>

</html>
